package com.pay.demo;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.pay.utils.FunctionUtils;

import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.StrUtil;

/**
 * standard.htm接口查询调用
 * @author Administrator
 *
 */
public class StandardDemo {

	public static void main(String[] args) throws IOException {
		Document doc = Jsoup.connect("https://consumeprod.alipay.com/record/standard.htm")
				.header("Cookie", "JSESSIONID=RZ18gLMDeuMi8mGKRTgPZPVqq4nXXjauthRZ18; mobileSendTime=-1; credibleMobileSendTime=-1; ctuMobileSendTime=-1; riskMobileBankSendTime=-1; riskMobileAccoutSendTime=-1; riskMobileCreditSendTime=-1; riskCredibleMobileSendTime=-1; riskOriginalAccountMobileSendTime=-1; cna=S0AmFFnxgkkCAXkNxeE67GoM; UM_distinctid=1675b44c2e42f4-05e63f299f5b-4e7a4a11-13c680-1675b44c2e5532; 3FV_lastvisit=3%091544069326%09%2Findex.php%3Fm%3Du%26a%3Dshowcredit; isg=BG9vMrgSb-7tdmxtbqAeLkqG64O5vMN7Nb9g1YH8C17l0I_SieRThm0BVoDLqJuu; unicard1.vm=\"K1iSL16QY1TOa0TWpNJUKg==\"; alipay=\"K1iSL16QY1TOa0TWpNJUKpg336kutio3Se7lnfOBbQ==\"; CLUB_ALIPAY_COM=2088722153529552; iw.userid=\"K1iSL16QY1TOa0TWpNJUKg==\"; ali_apache_tracktmp=\"uid=2088722153529552\"; session.cookieNameId=ALIPAYJSESSIONID; spanner=+WVaYozWu9KAsOCtMr485vF7dgzv+m6D4EJoL7C0n0A=; ctoken=N_DUHwYr_-JrWvTN; LoginForm=alipay_login_auth; zone=RZ18B; ALIPAYJSESSIONID=RZ18gLMDeuMi8mGKRTgPZPVqq4nXXjauthRZ18GZ00; rtk=sFHGqSjtW4VpIUON2FJXv2tCkUgy1XETv5OUoGfhYTGLgIn5o/b")
				.header("Upgrade-Insecure-Requests", "1")
				.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6788.400 QQBrowser/10.3.2727.400")
				.get();
		String title = doc.title();
		System.out.println(title);
		System.out.println();
		
		if(!"我的账单 - 支付宝".equals(title)) {
			return ; 
		}
		Elements supper = doc.select("a[seed=CR-AdvancedFilter]");
		if(supper == null || supper.size() < 1) {
			//警告
			return ; 
		}
		if(supper.get(0).text().indexOf("高级筛选") == -1) {
			return ;
		}
		//System.out.println(doc);
		Elements trElements = doc.select(".J-item");
		//System.out.println(trElements);
		for (Element trElement : trElements) {
			String consumeBizNo = trElement.select(".J-tradeNo").attr("title");
			String flagcode = trElement.select(".consume-title a").text();
			if(StrUtil.isBlank(flagcode)) {
				continue ;
			}
			String statusStr = trElement.select(".status .text-muted").get(0).text();
			if(!"交易成功".equals(statusStr)) {
				continue ;
			}
			String income = trElement.select(".amount-pay").text();
			if(StrUtil.isBlank(income)) {
				continue ;
			}
			if(income.indexOf("+") == -1) {
				continue ;
			}
			Pattern pattern = Pattern.compile("([0-9]{1,}[.]*[0-9]*)");
			Matcher matcher = pattern.matcher(income); //只取数字部分
			if(!matcher.find()) {
				continue; 
			}
			String name = trElement.select(".name .name").get(0).text();
			System.out.println(UnicodeUtil.toString(name));
			income = matcher.group(1) ;
			System.out.println(FunctionUtils.getFlagcode(flagcode));
			System.out.println(consumeBizNo);
			System.out.println(income);
		}
	}
}
